package com.jiang.专题.数组.Q13;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/16 17:43
 */
class Solution {
    public int romanToInt(String s) {
        int n = s.length();
        int res = getNumber(s.charAt(n - 1));
        for (int i = 0; i < n - 1; i++) {
            if (getNumber(s.charAt(i)) >= getNumber(s.charAt(i + 1))) {
                res += getNumber(s.charAt(i));
            } else {
                res -= getNumber(s.charAt(i));
            }
        }
        return res;
    }

    public static int getNumber(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
}
